package com.yf.exam.modules.tmpl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.log.annon.LogInject;
import com.yf.ability.log.enums.LogType;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdRespDTO;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.exam.modules.tmpl.dto.TmplDTO;
import com.yf.exam.modules.tmpl.dto.ext.TmplDetailDTO;
import com.yf.exam.modules.tmpl.dto.ext.TmplPreviewDTO;
import com.yf.exam.modules.tmpl.service.TmplService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* 试卷控制器
* </p>
*
* @author 聪明笨狗
* @since 2021-04-25 13:59
*/
@Api(tags={"试卷管理"})
@RestController
@RequestMapping("/api/tmpl/tmpl")
public class TmplController extends BaseController {

    @Autowired
    private TmplService baseService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @LogInject(title="添加或修改试卷", logType = LogType.TMPL)
    @RequiresPermissions(value = {"tmpl:add", "tmpl:update"}, logical = Logical.OR)
    @ApiOperation(value = "添加或修改")
    @PostMapping("/save")
    public ApiRest<BaseIdRespDTO> save(@RequestBody TmplDetailDTO reqDTO) {
        BaseIdRespDTO respDTO = baseService.save(reqDTO);
        return super.success(respDTO);
    }

    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @LogInject(title="复制试卷", logType = LogType.TMPL)
    @RequiresPermissions(value = {"tmpl:copy"})
    @ApiOperation(value = "复制试卷")
    @PostMapping("/copy")
    public ApiRest<BaseIdRespDTO> copy(@RequestBody BaseIdReqDTO reqDTO) {
        BaseIdRespDTO respDTO = baseService.copy(reqDTO.getId());
        return super.success(respDTO);
    }

    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @RequiresPermissions(value = {"tmpl:preview"})
    @ApiOperation(value = "预览试卷")
    @PostMapping("/preview")
    public ApiRest<TmplPreviewDTO> preview(@RequestBody BaseIdReqDTO reqDTO) {
        TmplPreviewDTO respDTO = baseService.preview(reqDTO.getId());
        return super.success(respDTO);
    }


    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @LogInject(title="删除试卷", logType = LogType.TMPL)
    @RequiresPermissions(value = {"tmpl:delete"})
    @ApiOperation(value = "批量删除")
    @PostMapping("/delete")
    public ApiRest delete(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.deleteByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @RequiresPermissions("tmpl:update")
    @ApiOperation(value = "查找详情")
    @PostMapping("/detail")
    public ApiRest<TmplDetailDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        TmplDetailDTO dto = baseService.findDetail(reqDTO.getId());
        return super.success(dto);
    }

    /**
     * 查找简略信息
     * @param reqDTO
     * @return
     */
    @RequiresPermissions(value = {"tmpl:update", "exam:update","exam:add"}, logical = Logical.OR)
    @ApiOperation(value = "查找简略信息")
    @PostMapping("/simple-info")
    public ApiRest<TmplDTO> simpleInfo(@RequestBody BaseIdReqDTO reqDTO) {
        TmplDTO dto = baseService.findSimpleInfo(reqDTO.getId());
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @RequiresPermissions(value = {"tmpl:list", "exam:update","exam:add"}, logical = Logical.OR)
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<TmplDTO>> paging(@RequestBody PagingReqDTO<TmplDTO> reqDTO) {
        //分页查询并转换
        IPage<TmplDTO> page = baseService.paging(reqDTO);
        return super.success(page);
    }
}
